package com.totoro.tyrank;

import com.totoro.tyrank.command.RankCommand;
import com.totoro.tyrank.handle.SkullCacheHandler;
import com.totoro.tyrank.hook.TyRankPapi;
import com.totoro.tyrank.listener.*;
import com.totoro.tyrank.manager.*;
import com.totoro.tyrank.sql.BaseDao;
import com.totoro.tyrank.utils.ColorUtil;
import com.zaxxer.hikari.HikariConfig;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

public final class TyRank extends JavaPlugin {

    private static TyRank INSTANCE;

    @Getter
    private static ConfigManager configManager;

    @Getter
    private static RankDataManager rankDataManager;

    @Getter
    private static PlayerDataManager playerDataManager;

    @Getter
    private static RankingManager rankingManager;

    @Getter
    private static LanguageManager languageManager;

    @Getter
    private static RankingTaskManager rankingTaskManager;

    @Getter
    private static SkullCacheHandler skullCacheHandler;

    @Getter
    private static RankingReceiveManager rankingReceiveManager;

    @Getter
    private static AttributeManager attributeManager;

    @Getter
    private static BaseDao baseDao;

    @Override
    public void onEnable() {
        INSTANCE = this;
        getServer().getConsoleSender().sendMessage(ColorUtil.color("&6[汤圆排行榜] &c正在加载系统配置..."));
        // 载入系统配置
        configManager = new ConfigManager(this);
        configManager.createConfig();
        configManager.reloadConfig();
        configManager.initMySQL();
        if(configManager.getConfigData().getMySQLData().getEnable()) {
            HikariConfig hikariConfig = new DatasourceManager(this).toHikariConfig(configManager.getConfigData().getMySQLData());
            try {
                baseDao = new BaseDao(hikariConfig);
                baseDao.initTables();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        getCommand("tyrank").setExecutor(new RankCommand());
        getCommand("tyrank").setTabCompleter(new RankCommand());
        getServer().getPluginManager().registerEvents(new CurrentRankInventoryListener(), this);
        getServer().getPluginManager().registerEvents(new AllRankInventoryListener(), this);
        getServer().getPluginManager().registerEvents(new HistoryRankInventoryListener(), this);
        getServer().getPluginManager().registerEvents(new RankContentInventoryListener(), this);
        getServer().getPluginManager().registerEvents(new AttributeReceiveInventoryListener(), this);
        getServer().getPluginManager().registerEvents(new RewardReceiveInventoryListener(), this);
        getServer().getConsoleSender().sendMessage(ColorUtil.color("&6[汤圆排行榜] &e系统配置加载完成"));

        getServer().getConsoleSender().sendMessage(ColorUtil.color("&6[汤圆排行榜] &c正在加载语言配置..."));
        languageManager = new LanguageManager(this);
        languageManager.createLanguageFile();
        languageManager.reloadLanguage();
        getServer().getConsoleSender().sendMessage(ColorUtil.color("&6[汤圆排行榜] &e语言配置加载完成"));

        getServer().getConsoleSender().sendMessage(ColorUtil.color("&6[汤圆排行榜] &c正在加载排行配置..."));

        rankingManager = new RankingManager(this);
        rankingManager.createRankFile();
        rankingManager.reloadRank();

        // 先初始化排行榜玩家头颅缓存处理器
        skullCacheHandler = new SkullCacheHandler(this);

        getServer().getConsoleSender().sendMessage(ColorUtil.color("&6[汤圆排行榜] &c正在加载所有排行榜数据和玩家数据..."));
        // 初始化排行榜持久化数据管理器
        rankDataManager = new RankDataManager(this);
        rankDataManager.loadRankData();
        // 初始化玩家管理器
        playerDataManager = new PlayerDataManager(this);
        playerDataManager.createPlayerFile();
        playerDataManager.loadRankPlayerData();
        getServer().getConsoleSender().sendMessage(ColorUtil.color("&6[汤圆排行榜] &e所有排行榜数据和玩家数据加载完成"));

        // 初始化排行榜任务管理器
        rankingTaskManager = new RankingTaskManager(this);
        rankingTaskManager.reloadRankingsTask();
        getServer().getConsoleSender().sendMessage(ColorUtil.color("&6[汤圆排行榜] &e排行配置加载完成"));

        // 初始化排名领取预览管理员
        rankingReceiveManager = new RankingReceiveManager(this);
        // 初始化属性管理器
        attributeManager = new AttributeManager(this);

        // 注册papi
        new TyRankPapi().register();
        // 注册加入退出->属性监听器
        getServer().getPluginManager().registerEvents(new JoinLeaveListener(), this);
        getServer().getConsoleSender().sendMessage(ColorUtil.color("&6[汤圆排行榜] &e插件加载完成"));
    }


    @Override
    public void onDisable() {
        if(null != baseDao) {
            baseDao.closeDataSource();
        }
        skullCacheHandler.shutdown();
        getServer().getConsoleSender().sendMessage(ColorUtil.color("&6[汤圆排行榜] &e插件卸载完成！"));
    }

    public static TyRank getInstance() {
        return INSTANCE;
    }
}
